package com.dog.basics.service.mtkj.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dog.basics.bean.dto.admin.mtkj.ShopGoodTypeFromDto;
import com.dog.basics.bean.dto.api.mtkj.ApiShopGoodTypeAddDto;
import com.dog.basics.bean.dto.api.mtkj.ApiShopGoodTypeEditDto;
import com.dog.basics.bean.model.mtkj.MtGoods;
import com.dog.basics.bean.model.mtkj.MtGoodsType;
import com.dog.basics.bean.model.mtkj.MtShopGoodType;
import com.dog.basics.bean.model.mtkj.MtShopping;
import com.dog.basics.bean.vo.admin.mtkj.ShopGoodTypeFromVo;
import com.dog.basics.bean.vo.api.mtkj.ApiShopGoodsTypeVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.mtkj.MtGoodsMapper;
import com.dog.basics.dao.mtkj.MtGoodsTypeMapper;
import com.dog.basics.dao.mtkj.MtShopGoodTypeMapper;
import com.dog.basics.dao.mtkj.MtShoppingMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.mtkj.IMtShopGoodTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 店铺商品类型表 服务实现类
 * </p>
 *
 * @author wph
 * @since 2020-11-09
 */
@Service
@AllArgsConstructor
public class MtShopGoodTypeServiceImpl extends ServiceImpl<MtShopGoodTypeMapper, MtShopGoodType> implements IMtShopGoodTypeService {

    private final MtGoodsMapper goodsMapper;
    private final MtShoppingMapper shoppingMapper;
    private final MtGoodsTypeMapper goodsTypeMapper;

    /**
     * 根据商铺id查询所有类型列表
     *
     * @param id
     * @return
     * @author hangdy
     */
    @Override
    public IPage<ApiShopGoodsTypeVo> findAllByShopId(PageParam page, Long id) {
        //查询商铺id检验是否是当前用户
        Validator.validateNotNull(id,"商铺信息不正确");
        MtShopping shopping = shoppingMapper.selectById(id);
        Validator.validateNotNull(shopping,"商铺信息不存在");
        //判断是否属于该用户
   //     Validator.validateEqual(shopping.getUserId(),SecurityUtils.getUser().getUserId(),"商铺信息有误");

        IPage iPage = this.page(page,Wrappers.<MtShopGoodType>lambdaQuery().eq(MtShopGoodType::getShopId, id));
        List<MtShopGoodType> shopGoodTypeList = iPage.getRecords();
        List<ApiShopGoodsTypeVo> list = shopGoodTypeList.stream().map(e -> BeanUtil.toBean(e, ApiShopGoodsTypeVo.class)).collect(Collectors.toList());
        iPage.setRecords(list);
        return iPage;
    }

    /**
     * 新增
     *
     * @param shopGoodTypeAddDto
     * @return
     * @author hangdy
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveShopGoodType(ApiShopGoodTypeAddDto shopGoodTypeAddDto) {
        MtShopGoodType shopGoodType = new MtShopGoodType();
        BeanUtil.copyProperties(shopGoodTypeAddDto, shopGoodType);

        //查询商铺id检验是否是当前用户
        Validator.validateNotNull(shopGoodTypeAddDto.getShopId(),"商铺信息不正确");
        MtShopping shopping = shoppingMapper.selectById(shopGoodTypeAddDto.getShopId());
        Validator.validateNotNull(shopping,"商铺信息不存在");
        //判断是否属于该用户
        Validator.validateEqual(shopping.getUserId(),SecurityUtils.getUser().getUserId(),"商铺信息有误");

        //判断商品类别是否存在
        MtGoodsType goodsType = goodsTypeMapper.selectById(shopGoodTypeAddDto.getTypeId());
        Validator.validateNotNull(goodsType,"商品类别不存在");

        shopGoodType.setCreateTime(DateUtil.currentSeconds());
        shopGoodType.setCreateBy(SecurityUtils.getUser().getUserId());
        Validator.validateTrue(this.save(shopGoodType), "新增店铺商品类型失败");
        return Boolean.TRUE;
    }

    /**
     * 修改
     *
     * @param shopGoodTypeEditDto
     * @return
     * @author hangdy
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editShopGoodType(ApiShopGoodTypeEditDto shopGoodTypeEditDto) {
        MtShopGoodType shopGoodType = this.getById(shopGoodTypeEditDto.getId());
        Validator.validateNotNull(shopGoodType, "未找该店铺商品类别");
        BeanUtil.copyProperties(shopGoodTypeEditDto, shopGoodType);

        //查询商铺id检验是否是当前用户
        Validator.validateNotNull(shopGoodTypeEditDto.getShopId(),"商铺信息不正确");
        MtShopping shopping = shoppingMapper.selectById(shopGoodTypeEditDto.getShopId());
        Validator.validateNotNull(shopping,"商品信息不存在");
        Validator.validateEqual(shopping.getUserId(),SecurityUtils.getUser().getUserId(),"商铺信息不正确");
        //判断是否属于该用户
        Validator.validateEqual(shopping.getUserId(),SecurityUtils.getUser().getUserId(),"商铺信息有误");

        //判断商品类别是否存在
        MtGoodsType goodsType = goodsTypeMapper.selectById(shopGoodTypeEditDto.getTypeId());
        Validator.validateNotNull(goodsType,"商品类别不存在");

        shopGoodType.setUpdateTime(DateUtil.currentSeconds());
        shopGoodType.setUpdateBy(SecurityUtils.getUser().getUserId());
        Validator.validateTrue(this.updateById(shopGoodType), "修改店铺商品类型失败");
        return Boolean.TRUE;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     * @author hangdy
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteGoodType(Long id) {
        MtShopGoodType shopGoodType = this.getById(id);
        Validator.validateNotNull(shopGoodType, "无此数据");

        //查询商铺id检验是否是当前用户
        Validator.validateEqual(shopGoodType.getCreateBy(),SecurityUtils.getUser().getUserId(),"无权操作");
        //检索商品商铺信息是否正确
        Validator.validateNotNull(shopGoodType.getShopId(),"商铺信息不正确");
        MtShopping shopping = shoppingMapper.selectById(shopGoodType.getShopId());
        Validator.validateNotNull(shopping,"商铺信息不正确");
        Validator.validateEqual(shopping.getUserId(),SecurityUtils.getUser().getUserId(),"商铺信息不正确");
        //判断是否属于该用户
        Validator.validateEqual(shopping.getUserId(),SecurityUtils.getUser().getUserId(),"商铺信息有误");

        //判读如果有该类型下有商品则无法删除 当前商铺下 的 类别
        List<MtGoods> goodsList = goodsMapper.selectList(Wrappers.<MtGoods>lambdaQuery()
                .eq(MtGoods::getShopGoodTypeId, shopGoodType.getId()));
        if(goodsList.size() != 0){
            throw new RuntimeException("该类别有商品无法删除");
        }

        Validator.validateTrue(this.removeById(id), "删除店铺商品类型失败");
        return Boolean.TRUE;
    }

    /**
     * admin服务
     *  超级管理员删除操作
     * @param: id
     * @returns
     * @author hangdy
     * @date: 2020/11/14 15:42
     */
    @Override
    public Boolean adminDeleteShopGoodType(Long id) {

        //2.判断是否存在该记录
        MtShopGoodType shopGoodType = this.getById(id);
        Validator.validateNotNull(shopGoodType,"未找到商铺商品类别");
        //3.判断是类别下是否有商品
        List<MtGoods> goodsList = goodsMapper.selectList(Wrappers.<MtGoods>lambdaQuery().eq(MtGoods::getShopGoodTypeId ,id));
        if(goodsList.size() != 0){
            throw new RuntimeException("该类别有商品无法删除");
        }
        //4.删除该记录
        this.removeById(id);
        return Boolean.TRUE;
    }

    /**
     * Adim服务
     *  查询商铺商品类别信息
     * @param: page
     * @param: dto
     * @returns
     * @author hangdy
     * @date: 2020/11/14 17:01
     */
    @Override
    public IPage<ShopGoodTypeFromVo> adminFindShopGoodTypeList(PageParam page, ShopGoodTypeFromDto dto) {
        IPage iPage = this.page(page,Wrappers.<MtShopGoodType>lambdaQuery()
                .like(ObjectUtil.isNotNull(dto.getName()),MtShopGoodType::getName,dto.getName())
        );

        List<MtShopGoodType> shopGoodTypeList = iPage.getRecords();
        List<ShopGoodTypeFromVo> goodTypeFromVos = shopGoodTypeList.stream().map(e -> {
            ShopGoodTypeFromVo vo = BeanUtil.copyProperties(e,ShopGoodTypeFromVo.class);
            MtShopping shopping = shoppingMapper.selectById(e.getShopId());
            if(shopping != null){
                vo.setShopName(ObjectUtil.isNotNull(shopping.getName()) ? shopping.getName() : "");
            }
            MtGoodsType goodsType = goodsTypeMapper.selectById(e.getTypeId());
            if(goodsType != null){
                vo.setTypeName(ObjectUtil.isNotNull(goodsType.getName()) ? goodsType.getName() : "");
            }
            return vo;
        }).collect(Collectors.toList());

        iPage.setRecords(goodTypeFromVos);

        return iPage;
    }
}
